﻿using System;
using System.Collections.Generic;

using Xin478.MySocket;
using Xin478.CSWork.Common;

namespace Xin478.CSWork.Server
{
    public class OnlineUser
    {
        static readonly object lockHelper = new object();
        Dictionary<int, int> onlineUsers;//<sessionid,userid>
        public OnlineUser()
        {
            this.onlineUsers = new Dictionary<int, int>();
        }
        public delegate void OnlineUserHandler(int sessionId, int userId);
        public delegate void AddOnlineUserHandler(Session session, EmployeeInfo employeeInfo);
        public event OnlineUserHandler DeletedOnlineUser;
        public event AddOnlineUserHandler AddedOnlineUser;

        public int FindSessionId(EmployeeInfo employeeInfo)
        {
            return FindSessionId(employeeInfo);
        }
        public int FindSessionId(int userId)
        {
            int sessionId = -1;

            if (onlineUsers.ContainsValue(userId))
            {
                Dictionary<int, int>.Enumerator etor = onlineUsers.GetEnumerator();
                while (etor.MoveNext())
                {
                    if (etor.Current.Value == userId)
                    {
                        sessionId = etor.Current.Key;
                        break;
                    }
                }
            }

            return sessionId;
        }
        public int FindUserId(Session session)
        {
            return FindUserId(session.ID.ID);
        }
        public int FindUserId(int sid)
        {
            return this.onlineUsers.ContainsKey(sid) ? onlineUsers[sid] : -1;
        }
        public bool IsOnline(int userId)
        {
            return this.onlineUsers.ContainsValue(userId);
        }
        public void Delete(EmployeeInfo employeeInfo)
        {
            Delete(employeeInfo.Id);
        }
        public void Delete(int userId)
        {
            int sid = -1, uid = userId;
            lock (lockHelper)
            {
                Dictionary<int, int>.Enumerator etor = onlineUsers.GetEnumerator();
                while (etor.MoveNext())
                {
                    if (etor.Current.Value == uid)
                    {
                        sid = etor.Current.Key;
                        onlineUsers.Remove(sid);
                        break;
                    }
                }
            }
            if (sid != -1 && DeletedOnlineUser != null) DeletedOnlineUser(sid, uid);
        }
        public void Delete(Session session)
        {
            int sid = session.ID.ID, uid = -1;
            if (onlineUsers.ContainsKey(sid))
            {
                lock (lockHelper)
                {
                    uid = onlineUsers[sid];
                    onlineUsers.Remove(sid);
                }
            }
            if (uid != -1 && DeletedOnlineUser != null) DeletedOnlineUser(sid, uid);
        }
        public void Add(Session session, EmployeeInfo employeeInfo)
        {
            if (employeeInfo == null) return;

            Delete(employeeInfo);
            lock (lockHelper)
            {
                this.onlineUsers.Add(session.ID.ID, employeeInfo.Id);
            }
            if (AddedOnlineUser != null) AddedOnlineUser(session, employeeInfo);
        }

    }

}
